/**
 * Copyright (c) 2006 RiseSoft Co.,Ltd  
 * $Header$
 */
package cn.newtest.bi.collection.formula.file.impl;

import java.util.List;

import net.risesoft.risebi.collection.formula.file.Creator;
import net.risesoft.risebi.collection.formula.file.FileCreator;
import net.risesoft.risebi.collection.formula.generate.FormulaGenerateException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

/**
 *
 *
 * @author <a href="mailto: chenjian@risesoft.net">陈健</a>
 * @version $Revision$
 * 
 */
@Repository(value="fileCreator")
public class FileCreatorImpl implements FileCreator {
	
	private Creator auditGroovyCreator;
	private Creator calculateGroovyCreator;
	private Creator validationJsCreator;
	
	@Autowired
	public void setAuditGroovyCreator(@Qualifier("auditGroovyCreator")Creator auditGroovyCreator) {
		this.auditGroovyCreator = auditGroovyCreator;
	}

	@Autowired
	public void setCalculateGroovyCreator(@Qualifier("calculateGroovyCreator")Creator calculateGroovyCreator) {
		this.calculateGroovyCreator = calculateGroovyCreator;
	}

	@Autowired
	public void setValidationJsCreator(@Qualifier("validationJsCreator")Creator validationJsCreator) {
		this.validationJsCreator = validationJsCreator;
	}

	/* (non-Javadoc)
	 * @see net.risesoft.risebi.collection.formula.file.FileCreator#createFile(java.lang.String)
	 */
	public void createFile(String templateGuid) throws Exception {
		List<FormulaGenerateException> auditExceptionList = auditGroovyCreator.doCreate(templateGuid);
		List<FormulaGenerateException> calculateExceptionList = calculateGroovyCreator.doCreate(templateGuid);
		List<FormulaGenerateException> validationExceptionList = validationJsCreator.doCreate(templateGuid);
		
		String errorString = makeErrorString(auditExceptionList);
		errorString += makeErrorString(calculateExceptionList);
		errorString += makeErrorString(validationExceptionList);
		
		if(!errorString.equals("")){
			throw new FormulaGenerateException(errorString);
		}
	}
	
	private String makeErrorString(List<FormulaGenerateException> exceptionList) {
		String errorString = "";
		for(int i=0;i<exceptionList.size();i++){
			FormulaGenerateException fge = exceptionList.get(i);
			errorString += fge.getMessage() + "\n\r";
		}
		return errorString;
	}

}
